package com.dsl.actuator;

import com.dsl.utils.CommunityUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @Description TODO
 * @Author dsl
 * @Date 2024/9/23 上午10:34
 * @Version 1.0
 */
@Component
@Endpoint(id = "database")
public class DatabaseEndpoint {

    private static final Logger logger = LoggerFactory.getLogger(DatabaseEndpoint.class);

    @Autowired
    private DataSource dataSource;

    public String checkConnection(){
        try{
            Connection conn = dataSource.getConnection();
        } catch( SQLException e){
            logger.error("获取连接失败：{}", e.getMessage());
            return CommunityUtil.getJSONString(1, "获取连接失败");
        }
            return CommunityUtil.getJSONString(0, "获取连接成功");
    }
}
